FROM --platform=linux/arm64 nvcr.io/nvidia/l4t-tensorrt:r8.2.1-runtime


# build参数
ARG user=root
ARG password=docker
ARG homedir=/${user}

# 元数据
LABEL maintainer="jiejie" 
LABEL email="jiejietop@gmail.com"
LABEL org.label-schema.schema-version="1.2.0"
LABEL org.label-schema.build-date="2023-03"
LABEL org.label-schema.name="rs:ros-arm-u18-xavier"
LABEL org.label-schema.description="Ubuntu 20.04, Linux Kernel 4.9.253, JetPack 4.6.1, CUDA 10.2, TensorRT 8.2.1, cuDNN 8 image"
LABEL org.label-schema.docker.cmd="\
docker run -it --rm --privileged \
    --net=host \
    --ipc=host \
    --pid=host \
    --gpus all \
    --runtime nvidia \
    -e DISPLAY=$DISPLAY \
    --platform linux/arm64 \
    -v ~/Documents:/root/workspace \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -v /run/dbus/system_bus_socket:/run/dbus/system_bus_socket:ro \
    -d rs:ros-arm-u18-xavier \
"

USER root

# tuna.tsinghua.edu.cn
# aliyun.com
RUN  sed -i s@/ports.ubuntu.com/@/mirrors.tuna.tsinghua.edu.cn/@g /etc/apt/sources.list \
    && sed -i s@/security.ubuntu.com/@/mirrors.tuna.tsinghua.edu.cn/@g /etc/apt/sources.list

# 更换源
RUN echo "deb https://repo.download.nvidia.com/jetson/common r32.7 main" > /etc/apt/sources.list.d/nvidia-l4t-apt-source.list \
    && echo "deb https://repo.download.nvidia.com/jetson/t194 r32.7 main" >> /etc/apt/sources.list.d/nvidia-l4t-apt-source.list

# 删除内部源
RUN rm /etc/apt/sources.list.d/cuda.list

# 添加公钥 参考： https://forums.developer.nvidia.com/t/adding-https-repo-download-nvidia-com-jetson-repositories-to-docker-image/121900/14
RUN apt-key adv --fetch-key https://repo.download.nvidia.com/jetson/jetson-ota-public.asc


RUN apt-get update

RUN apt-get install -y apt-utils software-properties-common locales sudo

RUN locale-gen zh_CN.UTF-8
ENV \
    LANG=zh_CN.UTF-8 \
    DEBIAN_FRONTEND=noninteractive \
    TERM=xterm


# 添加用户：赋予sudo权限，指定密码
# RUN useradd --create-home --no-log-init --shell /bin/bash ${user} \
#     && adduser ${user} sudo \
#     && echo "${user}:${password}" | chpasswd

# 指定容器起来的工作目录
WORKDIR ${homedir}/workspace

# =============安装基础依赖================
RUN apt-get remove -y --purge xserver-xorg

RUN apt-get install --no-install-recommends -y \
    gnupg2 \
    mesa-utils \
    sudo \
    tmux \
    wget \
    curl \
    apt-file \
    xserver-xorg

RUN dpkg-reconfigure xserver-xorg

RUN apt-get install -y \
    libpugixml-dev \
    libspdlog-dev \
    libjsoncpp-dev \
    libeigen3-dev \
    libzmq3-dev \
    mesa-common-dev \
    build-essential \
    libgl1-mesa-dev \
    libpqxx-dev \
    libdw-dev \
    xarclock \
    zsh \
    git \
    tig \
    g++ \
    cmake \
    dbus \
    tree \
    nmap \
    openssl \
    systemd \
    tshark \
    libpcap-dev \
    wireshark \
    iputils-ping \
    tmux \
    vim \
    libyaml-cpp-dev \
    libssl-dev \
    libpcl-dev \
    python3 \
    python3-pip \
    net-tools \
    htop \
    stress \
    gdb \
    libavresample-dev \
    tesseract-ocr \
    libgflags-dev \
    libgoogle-glog-dev \
    build-essential \
    pkg-config \
    libgtk2.0-dev \
    libavcodec-dev \
    libavformat-dev \
    libjpeg-dev \
    libswscale-dev \
    libtiff5-dev \
    libeigen3-dev \
    libwebp-dev \
    libavutil-dev \
    libglew-dev \
    libgtk-3-dev \
    libpng-dev \
    libpostproc-dev \
    libtbb-dev \
    libv4l-dev \
    libxvidcore-dev \
    libx264-dev \
    opencl-headers \
    qt5-default \
    zlib1g-dev \
    libgstreamer1.0-dev \
    libgstreamer-plugins-base1.0-dev \
    libprotobuf-dev \
    protobuf-compiler
    

# ########################## Linux 安装 cudnn ################################
RUN apt-get install -y \
    cuda-tools-10-2 \
    cuda-command-line-tools-10-2 \
    cuda-nvcc-10-2 \
    cuda-nvrtc-dev-10-2 \
    libcusolver-dev-10-2 \
    cuda-cupti-dev-10-2 \
    cuda-nvrtc-dev-10-2 \
    libcurand-dev-10-2 \
    cuda-driver-dev-10-2 \
    cuda-cudart-dev-10-2 \
    libcublas-dev \
    cuda-memcheck-10-2 \
    cuda-gdb-10-2 \
    cuda-cuobjdump-10-2 \
    cuda-cupti-dev-10-2 \
    libcufft-dev-10-2 \
    libcudnn8-dev \
    glmark2

# ########################## Linux 安装 cudnn ################################
RUN apt-get install -y \
    libnvinfer-dev \
    libnvinfer-plugin-dev \
    libnvonnxparsers-dev \
    libnvparsers-dev 
    

# ########################## Linux 环境管理 ################################
# 设置时区为上海
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime


# ########################## 安全环境管理 ################################
RUN curl -s https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# ########################## 安装Ubuntu中文字体支持 ################################
RUN git clone https://gitee.com/jiejietop/ubuntu-fonts.git /tmp/ubuntu-fonts
RUN cp -rdf /tmp/ubuntu-fonts/usr/share/fonts/my-ubuntu-fonts /usr/share/fonts/.


# ########################## zsh环境配置 ################################
RUN chsh -s /bin/zsh
RUN usermod -s /bin/zsh ${user}

USER ${user}
RUN git clone https://gitee.com/jiejieTop/ohmyzsh.git ${homedir}/.oh-my-zsh
RUN git clone https://gitee.com/jiejieTop/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-${homedir}/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
RUN git clone https://gitee.com/jiejieTop/zsh-autosuggestions.git ${ZSH_CUSTOM:-${homedir}/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
RUN git clone https://gitee.com/jiejieTop/git-open.git ${ZSH_CUSTOM:-${homedir}/.oh-my-zsh/custom}/plugins/git-open
RUN cp ${homedir}/.oh-my-zsh/templates/zshrc.zsh-template ${homedir}/.zshrc
RUN sed -i 's/plugins=(git)/plugins=(z git sudo extract vi-mode git-open safe-paste command-not-found colored-man-pages zsh-syntax-highlighting vscode zsh-autosuggestions)/g' ${homedir}/.zshrc \
    && sed -i 's/# DISABLE_AUTO_UPDATE="true"/DISABLE_AUTO_UPDATE="true"/g' ${homedir}/.zshrc \
    && sed -i 's/# DISABLE_AUTO_TITLE="true"/DISABLE_AUTO_TITLE="true"/g' ${homedir}/.zshrc


# ########################## tmux环境配置 ################################
RUN echo 'set-option -g mouse on' > ${homedir}/.tmux.conf
RUN echo 'set -g pane-border-status top' >> ${homedir}/.tmux.conf
RUN echo 'set-option -g allow-rename off' >> ${homedir}/.tmux.conf
RUN echo 'set -g pane-border-format "#{pane_index} #T #{pane_current_command}"' >> ${homedir}/.tmux.conf
RUN echo 'bind-key -T prefix R send-keys "renamePane" C-m' >> ${homedir}/.tmux.conf
RUN echo '' >> ${homedir}/.tmux.conf


# ##########################  安装 ROS melodic ################################
RUN sh -c '. /etc/lsb-release && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'
RUN curl -s https://gitee.com/jiejietop/ros_tools/raw/master/ros.acs | sudo apt-key add -
RUN apt-get update -y
RUN apt-get install ros-melodic-desktop-full -y

RUN echo "source /opt/ros/melodic/setup.bash" >> ${homedir}/.bashrc
RUN echo "source /opt/ros/melodic/setup.zsh" >> ${homedir}/.zshrc

RUN apt-get install -y \
    python3-rosdep \
    python3-wstool \
    build-essential \
    ros-melodic-jsk-visualization \
    ros-melodic-jsk-interactive \
    ros-melodic-jsk-interactive-marker \
    ros-melodic-jsk-interactive-test \
    ros-melodic-jsk-rqt-plugins \
    ros-melodic-jsk-rviz-plugins \
    ros-melodic-jsk-recognition-msgs \
    ros-melodic-jsk-recognition-utils \
    ros-melodic-jsk-topic-tools \
    ros-melodic-roseus \
    ros-melodic-image-view2 \
    ros-melodic-qt-gui-py-common \
    ros-melodic-rqt-gui-py \
    ros-melodic-rqt-image-view \
    ros-melodic-rqt-plot \
    ros-melodic-sound-play \
    ros-melodic-posedetection-msgs

# 配置cv_bridge
RUN ln -s /usr/include/opencv4 /usr/include/opencv

# RUN rosdep init
# RUN rosdep update

# ########################## Python3环境管理 ################################
RUN apt-get install python3-pip -y
RUN wget https://bootstrap.pypa.io/get-pip.py
RUN python3 get-pip.py
RUN python3 -m pip install --upgrade pip
RUN pip3 install pyyaml

RUN apt-get remove python3.8 -y
# 修复python3的软连接问题
RUN rm /usr/bin/python3 && ln -s /usr/bin/python3.6  /usr/bin/python3
RUN rm /usr/bin/python && ln -s /usr/bin/python2.7 /usr/bin/python


# ##########################  安装 eCAL ################################
# RUN sh -c '. /etc/lsb-release && echo "deb http://ppa.launchpad.net/ecal/ecal-latest/ubuntu `lsb_release -cs` main" > /etc/apt/sources.list.d/ecal-latest.list'

RUN add-apt-repository ppa:ecal/ecal-latest -y
RUN apt-get update -y
RUN apt-get install ecal -y

# Set network_enabled = true in ecal.ini.
# You can omit this, if you only need local communication.
RUN awk -F"=" '/^network_enabled/{$2="= true"}1' /etc/ecal/ecal.ini > /etc/ecal/ecal.tmp && \
	rm /etc/ecal/ecal.ini && \
	mv /etc/ecal/ecal.tmp /etc/ecal/ecal.ini 

# Print the eCAL config
RUN ecal_config

# ########################## 更新系统的头文件 ################################


RUN sed -i s@flann/ext/lz4.h@lz4.h@g /usr/include/flann/util/serialization.h
RUN sed -i s@flann/ext/lz4hc.h@lz4hc.h@g /usr/include/flann/util/serialization.h

# ########################## 添加OpenCV软连接 ################################

RUN ln -s /usr/include/opencv4 /usr/include/opencv

USER ${user}
